﻿using System;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using DataProcessor.EF;
using DataProcessor.Process;
using Hjt212Parser;
using log4net;

namespace DataProcessor
{
    public partial class ServiceProcessor : ServiceBase
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ServiceProcessor));

        public ServiceProcessor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                Log.Info("准备启动服务...");
                
                //Util.Init();
                new Thread(ProcessDataMsg).Start();
                new Thread(ProcessCmdMsg).Start();
                
                new Thread(Downline).Start();
               // new Thread(Downline_2).Start();
                Log.Info("完成启动服务...");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }


        }
        private void Downline(object state)
        {
            while (true)
            {
                Process.Downline.Downline3012();
                Process.Downline.Downline3012Helper();
                Process.Downline.Downline3022();
                Process.Downline.Downline3097();
 
                ThreadUtil.Sleep(1000);
            }
        }

      
        protected override void OnStop()
        {
            Log.Info("The processor Stoped!");
            Environment.Exit(0);
        }
        private void ProcessDataMsg(object state)
        {
            while (true)
            {
                ProcessMsg("2011");
                ProcessMsg("2051");
                ProcessMsg("2061");
                ProcessMsg("2031");
                ThreadUtil.Sleep(1000);
            }
        }
        private void ProcessCmdMsg(object obj)
        {
            while (true)
            {
                ProcessMsg("0000");
                ProcessMsg("9999");
                ProcessMsg("9011");
                ProcessMsg("9012");
                ProcessMsg("9014");
                ProcessMsg("3097");
                ProcessMsg("3071");
                ProcessMsg("2021");
                ProcessMsg("2041");
                ProcessMsg("2072");
                ProcessMsg("3020");
                ProcessMsg("4003");
                ProcessMsg("8013");
                ProcessMsg("9021");
                ThreadUtil.Sleep(1000);
            }
        }

        private static void ProcessMsg(string cn)
        {
            try
            {
                using (var gCtx = new GatewayContext())
                {
                    var notProcessed = "NotProcessed";
                    var items =
                        gCtx.received_msg.Where(t => t.is_length_right)
                            .Where(t => t.processed_flag.Contains(notProcessed))
                            .Where(t => t.cn.Equals(cn))
                            .OrderBy(t => t.id)
                            .Take(100)
                            .ToList();
                    foreach (var item in items)
                    {
                        Log.Info("Processing:" + item.received_string);
                        item.processed_flag = "ProcessingFinished";
                        item.processed_flag_2 = "ProcessingFinished";
                        item.processed_flag_3 = "ProcessingFinished";
                        ProcessorMonitor.Process(item.received_string);
                        switch (item.cn)
                        {
                            case "2011":
                                DataInstructionProcessor.Process2011(item.received_string);
                                break;
                            case "2051":
                                DataInstructionProcessor.Process2051(item.received_string);
                                break;
                            case "2061":
                                DataInstructionProcessor.Process2061(item.received_string);
                                break;
                            case "2031":
                                DataInstructionProcessor.Process2031(item.received_string);
                                DataInstructionProcessor.Process2031Month(item.received_string);
                                DataInstructionProcessor.Process2031Year(item.received_string);
                                break;
                            case "9011":
                                ResponseProcessor.Process9011(item.received_string);
                                break;
                            case "9012":
                                ResponseProcessor.Process9012(item.received_string);
                                break;
                            case "9014":
                                ResponseProcessor.Process9014(item.received_string);
                                break;
                            case "3097":
                                CmdInstructionProcessor.Process3097(item.received_string);
                                break;
                            case "3071":
                                CmdInstructionProcessor.Process3071(item.received_string);
                                break;
                            default:
                                break;
                                
                        }
                        if (item.cn.Contains("0000"))
                        {
                            ProcessorStatus.ProcessOffline(item.received_string);
                        }
                        else
                        {
                            ProcessorStatus.ProcessOnline(item.received_string);
                        }
                    }
                    gCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
